<?php
	include_once './config.php';
	include_once './include/ez_sql_core.php';
	include_once './include/ez_sql_mysql.php';
	include_once './include/dbcconv.php';
	$db = new ezSQL_mysql($config['db']['dbuser'],$config['db']['dbpw'],$config['db']['dbname'],$config['db']['dbhost']);
	$db->query('set names '.$config['db']['dbcharset']);
	switch($_REQUEST['type'])
	{
		case 'list':
			extract($_REQUEST);
			if($action == 'list'){
				$sql = "select id,title,time,contect FROM contents WHERE lang = '".$lang."' AND part = '".$part."' AND del = 0 LIMIT ".($page-1)*$rows." , ".$rows;
				$results=$db->get_results($sql);
				$output = array();
				if($db->num_rows > 0){
					foreach ( $results as $result ){
						$row = array();
						$row['id']=$result->id;
						$row['title']=$result->title;
						$row['time']=$result->time;
						$row['contect']=$result->contect;
						array_push($output,$row);
					}
				}
				$sql = "select count(*) FROM contents WHERE lang = '".$lang."' AND part = '".$part."' AND del = 0 ";
				$total = $db->get_var($sql);
				header("Cache-Control: no-cache");
				echo json_encode(array('total'=>$total,'rows' => $output));
				die();
			}else if ($action == 'delete'){
				$sql = "select map from contents where id = ".$id;
				$result = $db->get_var($sql);
				$sql = "update contents set del = 1 where id in (".$id.",".$result.")";
				$db->query($sql);
				if($db->rows_affected > 0){
					header("Cache-Control: no-cache");
					echo json_encode("success");
					die();
				}else{
					header("Cache-Control: no-cache");
					echo json_encode("fail");
					die();
				}
			}
		break;
		case 'editor':
			//session_start();
			extract($_REQUEST);
			if ($action=='view'){
				if (!empty($lang)){
					$sql = "SELECT id,title,time,contect FROM contents WHERE lang = '".$lang."' AND part = '".$part."' AND del = 0 LIMIT 1";
					$result=$db->get_row($sql);
					if ($db->num_rows > 0){
						$output = array('id'=>$result->id,'title'=>$result->title,'time'=>$result->time,'contect'=>$result->contect);
						header("Cache-Control: no-cache");
						echo json_encode($output);
						die();
					}else{
						header("Cache-Control: no-cache");
						echo json_encode("fail");
						die();
					}
				 }else{
					header("Cache-Control: no-cache");
					echo json_encode("fail");
					die();
				 }
			}
			if ($action=='new'){
				die();
			}
			if ($action=='edit'){
				if (!empty($lang)){
					$sql = "SELECT title,contect FROM contents WHERE id = ".$id;
					$result=$db->get_row($sql);
					if ($db->num_rows > 0){
						$output = array('title'=>$result->title,'contect'=>$result->contect);
						header("Cache-Control: no-cache");
						echo json_encode($output);
						die();
					}else{
						header("Cache-Control: no-cache");
						echo json_encode("fail");
						die();
					}
				 }else{
					header("Cache-Control: no-cache");
					echo json_encode("fail");
					die();
				 }
			}
			if ($action=='save'){
				if(in_array($_REQUEST['part'],array('news','services','projects'))){
					if(empty($id)){
						$sql = "SELECT id FROM contents where 1 <> 1";
					}else{
						$sql = "SELECT id,map,title,time,contect FROM contents WHERE id = ".$id;
					}
				}else{
					$sql = "SELECT id,map,title,time,contect FROM contents WHERE lang = '".$lang."' AND part = '".$part."' AND del = 0 LIMIT 1";
				}	
				$result=$db->get_row($sql);
				if ($db->num_rows > 0){
					$sql = "update contents SET title = '".$title."' , contect = '".$contect."' , time = now() where id = ".$result->id;
					$db->query($sql);
					if ($db->rows_affected > 0){
						if ($langchange == 1){
							$lang_conv = ($lang == 'sc')?0:1;
							$title = dbcconv($title,$lang_conv);
							$contect = dbcconv($contect,$lang_conv);
							$sql = "update contents SET title = '".$title."' , contect = '".$contect."' , time = now() where id = ".$result->map;
							$db->query($sql);
						}
						if ($db->rows_affected > 0){
							header("Cache-Control: no-cache");
							echo json_encode('success');
							die();
						}else{
							header("Cache-Control: no-cache");
							echo json_encode('fail');
							die();
						}
					}else{
						header("Cache-Control: no-cache");
						echo json_encode('fail');
						die();
					}
				}else{
					$sql = "insert into contents (lang,part,title,time,publisher,contect,del) values ('".$lang."','".$part."','".$title."',now(),'admin','".$contect."',0)";
					$db->query($sql);
					if ($db->rows_affected > 0){
						$id_sc = $db->insert_id;
						$lang_oth = ($lang == 'sc')?'tc':'sc';
						$lang_conv = ($lang == 'sc')?0:1;
						$title_oth = dbcconv($title,$lang_conv);
						$contect_oth = dbcconv($contect,$lang_conv);
						$sql = "insert into contents (map,lang,part,title,time,publisher,contect,del) values ('".$id_sc."','".$lang_oth."','".$part."','".$title_oth."',now(),'admin','".$contect_oth."',0)";
						$db->query($sql);
						$id_oth = $db->insert_id;
						$sql = "update contents set map = ".$id_oth." where id = ".$id_sc;
						$db->query($sql);
						if ($db->rows_affected > 0){
							header("Cache-Control: no-cache");
							echo json_encode('success');
							die();
						}else{
							header("Cache-Control: no-cache");
							echo json_encode('fail');
							die();
						}
					}else{
						header("Cache-Control: no-cache");
						echo json_encode('fail');
						die();
					}
				}
			}
		break;
		case 'img':
			extract($_REQUEST);
			if($action == 'list'){
				$sql = "select id,title,url,src,queue from pic where lang = '".$lang."' and del = 0 LIMIT ".(($page-1)*$rows)." , ".$rows;
				$results=$db->get_results($sql);
				$output = array();
				if($db->num_rows > 0){
					foreach ( $results as $result ){
						$row = array();
						$row['id']=$result->id;
						$row['title']=$result->title;
						$row['url']=$result->url;
						$row['src']=$result->src;
						$row['queue']=$result->queue;
						array_push($output,$row);
					}
				}
				$sql = "select count(*) FROM  pic where lang = '".$lang."' and del = 0";
				$total = $db->get_var($sql);
				header("Cache-Control: no-cache");
				echo json_encode(array('total'=>$total,'rows' => $output));
				die();
			}
			if ($action=='new'){
				die();
			}
			if ($action=='edit'){
				if (!empty($lang)){
					$sql = "SELECT title,url,src FROM pic WHERE id = ".$id;
					$result=$db->get_row($sql);
					if ($db->num_rows > 0){
						$output = array('title'=>$result->title,'link'=>$result->url,'img'=>$result->src);
						header("Cache-Control: no-cache");
						echo json_encode($output);
						die();
					}else{
						header("Cache-Control: no-cache");
						echo json_encode("fail");
						die();
					}
				 }else{
					header("Cache-Control: no-cache");
					echo json_encode("fail");
					die();
				 }
			}
			if ($action=='save'){
				if(empty($id)){
					$sql = "SELECT id FROM pic where 1 <> 1";
				}else{
					$sql = "SELECT id,map FROM pic WHERE id = ".$id;
				}
				$result=$db->get_row($sql);
				if ($db->num_rows > 0){
					$sql = "update pic SET title = '".$title."' , url = '".$link."' , src = '".$img."' ,time = now() where id = ".$result->id;
					$db->query($sql);
					if ($db->rows_affected > 0){
						if ($langchange == 1){
							$lang_conv = ($lang == 'sc')?0:1;
							$title = dbcconv($title,$lang_conv);
							if($lang == 'tc'){
								copy("./upload/index_img/tc/big/".$img,"./upload/index_img/sc/big/".$img);
								copy("./upload/index_img/tc/small/".$img,"./upload/index_img/sc/small/".$img);
							}else{
								copy("./upload/index_img/sc/big/".$img,"./upload/index_img/tc/big/".$img);
								copy("./upload/index_img/sc/small/".$img,"./upload/index_img/tc/small/".$img);
							}
							$sql = "update pic SET title = '".$title."' , url = '".$link."' , src = '".$img."' ,time = now() where id = ".$result->map;
							$db->query($sql);
						}
						if ($db->rows_affected > 0){
							header("Cache-Control: no-cache");
							echo json_encode('success');
							die();
						}else{
							header("Cache-Control: no-cache");
							echo json_encode('fail');
							die();
						}
					}else{
						header("Cache-Control: no-cache");
						echo json_encode('fail');
						die();
					}
				}else{
					$sql = "insert into pic (lang,title,src,url,queue,time,del) values ('".$lang."','".$title."','".$img."','".$link."',0,now(),0)";
					$db->query($sql);
					if ($db->rows_affected > 0){
						$id_sc = $db->insert_id;
						$lang_oth = ($lang == 'sc')?'tc':'sc';
						$lang_conv = ($lang == 'sc')?0:1;
						$title_oth = dbcconv($title,$lang_conv);
						$sql = "insert into pic (map,lang,title,src,url,queue,time,del) values (".$id_sc.",'".$lang_oth."','".$title_oth."','".$img."','".$link."',0,now(),0)";
						$db->query($sql);
						$id_oth = $db->insert_id;
						$sql = "update pic set map = ".$id_oth." where id = ".$id_sc;
						$db->query($sql);
						if ($db->rows_affected > 0){
							if($lang == 'sc'){
								copy("./upload/index_img/sc/big/".$img,"./upload/index_img/tc/big/".$img);
								copy("./upload/index_img/sc/small/".$img,"./upload/index_img/tc/small/".$img);
							}else{
								copy("./upload/index_img/tc/big/".$img,"./upload/index_img/sc/big/".$img);
								copy("./upload/index_img/tc/small/".$img,"./upload/index_img/sc/small/".$img);
							}
							header("Cache-Control: no-cache");
							echo json_encode('success');
							die();
						}else{
							header("Cache-Control: no-cache");
							echo json_encode('fail');
							die();
						}
					}else{
						header("Cache-Control: no-cache");
						echo json_encode('fail');
						die();
					}
				}
			}
			if ($action == 'editqueue'){
				$sql = "update pic set queue = ".$queue." where id = ".$id;
				$db->query($sql);
				if ($db->rows_affected > 0){
					header("Cache-Control: no-cache");
					echo json_encode('success');
					die();
				}else{
					header("Cache-Control: no-cache");
					echo json_encode('fail');
					die();
				}
			}
			if ($action == 'delete'){
				$sql = "update pic set del = 1 where id = ".$id;
				$db->query($sql);
				if($db->rows_affected > 0){
					header("Cache-Control: no-cache");
					echo json_encode("success");
					die();
				}else{
					header("Cache-Control: no-cache");
					echo json_encode("fail");
					die();
				}
			}
		break;
		case 'changepassword':
			extract($_REQUEST);
			$sql = "select username from user where password = '".md5($oldpass)."'";
			$result=$db->get_row($sql);
			if ($db->num_rows > 0){
				$sql = "update user set password = '".md5($newpass)."' where username = '".$result->username."'";
				$db->query($sql);
				if ($db->rows_affected > 0){
					header("Cache-Control: no-cache");
					echo json_encode('success');
					die();
				}else{
					header("Cache-Control: no-cache");
					echo json_encode('fail');
					die();
				}
			}else{
				header("Cache-Control: no-cache");
				echo json_encode('fail');
				die();
			} 
		break;
		case 'has_login':
			session_start();
			if($_SESSION['username'] != '') {
				$sql = "update user set lasttime = now() where username = '".$_SESSION['username']."'";
				$db->query($sql);
				if ($db->rows_affected > 0){
					echo json_encode('success');
					die();
				}else{
					echo json_encode('fail');
					die();
				}
			}else{
				echo json_encode('fail');
				die();
			}
		break;
		case 'login':
			extract($_REQUEST);
			$sql = "select username from user where password = '".md5($password)."'";
			$result=$db->get_row($sql);
			if ($db->num_rows > 0){
				$sql = "update user set lasttime = now() where username = '".$result->username."'";
				$db->query($sql);
				if ($db->rows_affected > 0){
					session_start();
					$_SESSION['username'] = $result->username;
					header("Cache-Control: no-cache");
					echo json_encode('success');
					die();
				}else{
					header("Cache-Control: no-cache");
					echo json_encode('fail');
					die();
				}
			}else{
				header("Cache-Control: no-cache");
				echo json_encode('fail');
				die();
			}
		break;
        case 'logout':
			session_start();
			session_destroy();
			echo json_encode('success');
			die();
		break;
	}
?>
